{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Subplots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Subplots\n",
    "\n",
    "When plotting multiple columns, hvPlot will overlay the plots onto one axis by default so that they can be compared easily in a compact format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xarray as xr\n",
    "import hvplot.pandas  # noqa\n",
    "import hvplot.xarray  # noqa\n",
    "\n",
    "from hvplot.sample_data import airline_flights, us_crime\n",
    "\n",
    "us_crime.hvplot(x='Year', y=['Burglary rate', 'Violent Crime rate', 'Robbery rate'], value_label='Rate')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you wish, you can instead set `subplots=True` to split each column into its own separate plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "us_crime.hvplot(x='Year', y=['Burglary rate', 'Violent Crime rate', 'Robbery rate'],\n",
    "                value_label='Rate', subplots=True, width=300, height=200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, the subplots will have linked, normalized axes, to facilitate comparing the numerical values across plots (try panning or zooming in any one of the plots, to see how the others change.) \n",
    "\n",
    "However, if the data covers widely different numerical ranges, you can specify `shared_axes=False` to give each plot its own range:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "us_crime.hvplot(x='Year', y=['Robbery',  'Robbery rate', 'Burglary', 'Burglary rate'], \n",
    "                width=350, height=300, subplots=True, shared_axes=False).cols(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(Notice the very different y axis ranges between the plots.) Here we also specified `.cols(2)` to allow up to two plots per line, wrapping the rest onto subsequent rows.\n",
    " \n",
    "You can use the `subplots=True` (and `shared_axes` if desired) arguments when using the ``by`` keyword as well, if you want to group the data along a dimension: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flights = airline_flights.read()\n",
    "\n",
    "flight_subset = flights[flights.carrier.isin(['OH', 'F9', 'US'])].sample(2000)\n",
    "\n",
    "flight_subset.hvplot.scatter(x='arrdelay', y='depdelay', by='carrier',\n",
    "                             subplots=True, width=250, height=250, alpha=0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Grids\n",
    "\n",
    "\n",
    "`subplot=True` lays out plots sequentially, breaking lines if requested with the `.cols()` method but otherwise formatting each plot independently. You can instead arrange multidimensional data into an explicit 1D row of plots or a 2D grid of plots with shared axes, to allow easier comparisons across large numbers of plots.  To make a row or grid plot, just specify the ``col`` keyword and add a ``row`` keyword if you want a grid:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flight_subset.sort_values('dayofweek').hvplot.scatter(x='arrdelay', y='depdelay', \n",
    "    row='dayofweek', col='carrier', alpha=0.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(Just declaring `row` to get a single column is not currently supported.) Here you can see that compared to the subplot versions above, the axis ticks and labels are shared to save space and make comparisons easier.\n",
    "\n",
    "If you do not require an x axis and y axis for each plot at all, you can disable it with the ``xaxis`` and ``yaxis`` options:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "air_ds = xr.tutorial.open_dataset('air_temperature').load()\n",
    "\n",
    "air_ds.air.isel(time=slice(0, 5)).hvplot(col='time', xaxis=False, yaxis=False, colorbar=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using subplots and grids in this way is supported throughout the hvPlot API, making it simple to determine how you want your data laid out and overlaid."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
